﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.APP_UI.Common;
//using KPIS.Utils;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI
{
    public partial class BGM_Mas_Option_1 : System.Web.UI.Page
    {
        private IOptionBLL optCtrl = new OptionBLL();
        private IOptionTypeBLL optTypeCtrl = new OptionTypeBLL();
        private int optType;
        private RadTreeView tParent;
        private Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            IList<OptionTypeInfo> optTypeList = optTypeCtrl.Search(optType);

            if (!IsPostBack)
            {
                LoadYear();
                SetDisplayControl(optTypeList[0]);
                LoadDataGrid();
            }
        }

        private void LoadYear()
        {
            int CurrYear = SystemInfo._CurrentYear;
            ddlYear.Items.Clear();

            for (int i = CurrYear + 5; i >= CurrYear - 5; i--)
            {
                ddlYear.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }
            ddlYear.SelectedValue = CurrYear.ToString();
        }

        private void SetDisplayControl(OptionTypeInfo optTypeList)
        {
            lblHeader.Text = optTypeList.Name;
            lbConfigName.Text = optTypeList.LabelName;
            lbConfigParent.Text = optTypeList.LabelName;
            Grid.Columns[3].HeaderText = optTypeList.LabelName;
            pnParent.Visible = (optTypeList.HasChild == 1);
            ddlYear.Visible = !(optTypeList.HasChild == 1);
        }

        private void LoadDataGrid()
        {
            LoadDataGrid(0);
        }

        private void LoadDataGrid(int seq)
        {
            Nullable<int> nYear = null;

            if (ddlYear.Visible)
            {
                nYear = Convert.ToInt32(ddlYear.SelectedValue);
            }

            Grid.DataSource = optCtrl.Search(seq, optType, nYear);
            Grid.DataBind();

            //tParent.DataSource = optCtrl.TreeView(optType);
            //tParent.DataBind();
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            optType = Convert.ToInt32(Page.Request["optType"].ToString());
            tParent = ((RadTreeView)cbParent.Items[0].FindControl("tParent"));

            CmdCtrl.Inserted += new EventHandler(OnInsert);
            CmdCtrl.Updated += new EventHandler(OnUpdate);
            CmdCtrl.Deleted += new EventHandler(OnDelete);
        }
        #endregion

        private void OnInsert(object sender, EventArgs e)
        {
           bool isDuplicated = optCtrl.CheckDuplicateCode(txtCode.Text);

            if (isDuplicated == false)
            {
                string str = txtCode.Text.Trim();
                Double Num;
                bool isNum = Double.TryParse(str, out Num);
                if(isNum == false)
                {
                    main.MessageAlert(Page, "กรอกเป็นตัวเลขเท่านั้น");
                }
                else
                {
            int parent = 0;
            Nullable<int> nYear = null;

            if (tParent.SelectedValue.Length > 0)
                parent = Convert.ToInt32(tParent.SelectedValue);

            if (ddlYear.Visible)
                nYear = Convert.ToInt32(ddlYear.SelectedValue);

            optCtrl.Insert(txtCode.Text, txtName.Text, txtDescr.Text, parent, optType, nYear);
            LoadDataGrid();
            CmdCtrl.RefreshGrid();
                }
            }
            else
            {
                main.MessageAlert(Page, "รหัสซ้ำ");
            }
        }

        private void OnUpdate(object sender, EventArgs e)
        {
            bool isDuplicated = optCtrl.CheckDuplicateCode(txtCode.Text);

            if (isDuplicated == false || txtCode.Text == hdCode.Text)
            {
                string str = txtCode.Text.Trim();
                Double Num;
                bool isNum = Double.TryParse(str, out Num);
                if (isNum == false)
                {
                    main.MessageAlert(Page, "กรอกเป็นตัวเลขเท่านั้น");
                }
                else
                {

            int parent = 0;
            Nullable<int> nYear = null;

            if (tParent.SelectedValue.Length > 0)
                parent = Convert.ToInt32(tParent.SelectedValue);

            if (ddlYear.Visible)
                nYear = Convert.ToInt32(ddlYear.SelectedValue);

            optCtrl.Update(Convert.ToInt32(hdSeq.Text), txtCode.Text, txtName.Text, txtDescr.Text, parent, optType);
            LoadDataGrid();
            CmdCtrl.MapControl();
             }
            }
            else
            {
                main.MessageAlert(Page, "รหัสซ้ำ");
            }
        }

        private void OnDelete(object sender, EventArgs e)
        {
            int nRowSelected = 0;

            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
                {
                    optCtrl.Delete(Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"]));

                    nRowSelected++;
                }
            }
            else
            {
                optCtrl.Delete(Convert.ToInt32(hdSeq.Text));
            }

            LoadDataGrid();
            CmdCtrl.RefreshGrid();
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "TargetClick")
            {
                IList<OptionInfo> optList = optCtrl.Search(Convert.ToInt32(((Label)e.Item.FindControl("targetSequence")).Text)
                    , optType, Convert.ToInt32(ddlYear.SelectedValue));

                if (optList.Count > 0)
                {
                    hdSeq.Text = optList[0].Sequence.ToString();

                    // เก็บไว้เพื่อเอาไปเช็คตอน update
                    hdCode.Text = optList[0].Code;

                    txtCode.Text = optList[0].Code;
                    txtName.Text = optList[0].Name;
                    txtDescr.Text = optList[0].Description;

                    //for(int i = 0; i < tParent.Nodes.Count ; i++)
                    //{
                    //    tParent.Nodes[i].Value = optList[0].Parent.Sequence.ToString();
                    //}
                    //if (optList[0].Parent != null)
                    //{
                    //    cbExpense.LoadExpenseComboBox(0, "", "", "", optType, 0, optList[0].ExpenseGroup.Sequence);
                    //    parentCombo.SelectedValue = optList[0].Parent.Sequence.ToString();
                    //}
                    CmdCtrl.MapControl();
                }
            }
            else
                LoadDataGrid();
        }

        protected void ddlYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //if (ddlYear.Text.Length > 0)
            LoadDataGrid();
        }
    }
}
